from ..common import *

def set_path_to_urlpy(url: str, url_path: str, path_type: int, view_name: str, view_path: str, alia: str, already_url: str=None)->None:
    """将路由代码写入路由文件
    
        path_type: 0表示函数，1表示类
        （默认模板不涉及 re_path() 函数）
    """
    content = djangotools.get_list_patt_content(retools.PATT_URLPATTERNS, url_path)

    if not already_url:
        view =  os.path.basename(view_path).split('.')[0] # 默认 urls
        if 0 == path_type:
            temp = view + '.' + view_name # 函数视图
        else:
            temp = view + '.' + view_name + '.as_view()' # 类视图
        new_content = content + f"    path('{url}', {temp}, name='{alia}'),\n" # 即将写入的路由
    else:
        new_content = content + f"    {already_url},\n" # 即将写入的路由
    write_file(url_path, read_file(url_path).replace(content, new_content)) # 注册路由
